<?php

function transcribe($aList, $aIsTopLevel = true)
{
   $gpcList = array();
   $isMagic = false;

   foreach ($aList as $key => $value) {
       if (is_array($value)) {
           $decodedKey = ($isMagic && !$aIsTopLevel)?stripslashes($key):$key;
           $decodedValue = transcribe($value, false);
       } else {
           $decodedKey = stripslashes($key);
           $decodedValue = ($isMagic)?stripslashes($value):$value;
       }
       $gpcList[$decodedKey] = $decodedValue;
   }
   return $gpcList;
}

//$_GET = transcribe( $_GET );
//$_POST = transcribe( $_POST );
//$_REQUEST = transcribe( $_REQUEST );

// request
function v( $str )
{
	return isset( $_REQUEST[$str] ) ? $_REQUEST[$str] : false;
}

// session
function s( $str){
	return isset( $_SESSION[$str] ) ? $_SESSION[$str] : false;
}

// clear str
function z( $str )
{
	return strip_tags( $str );
}

// config
function c( $str )
{
	return isset( $GLOBALS['config'][$str] ) ? $GLOBALS['config'][$str] : false;
}

// global
function g( $str )
{
	return isset( $GLOBALS[$str] ) ? $GLOBALS[$str] : false;
}

// trim
function t( $str )
{
	return trim($str);
}

// urlencode
function u( $str )
{
	return urlencode( $str );
}

// 省略sharp参数默认绘制view/controler.tpl.html, 可以自行设定
function render( $data = NULL , $sharp = "" )
{
	if( $sharp == "" ) $sharp = $GLOBALS['c'];
	$layout = 'web';
	$GLOBALS['sharp'] = $sharp;
	

	$layout_file = AROOT . 'view/'. $sharp . '.tpl.html';
	if( file_exists( $layout_file ) )
	{
		@extract( $data ); // extract() 函数从数组中把变量导入到当前的符号表中
		require( $layout_file );
	}
	else
	{
		echo "no such file~ ".$layout_file;
	}
}

function ajax_echo( $info )
{
	if( !headers_sent() )
	{
		header("Content-Type:text/html;charset=utf-8");
		header("Expires: Thu, 01 Jan 1970 00:00:01 GMT");
		header("Cache-Control: no-cache, must-revalidate");
		header("Pragma: no-cache");
	}

	echo $info;
}


function info_page( $info , $title = '系统消息' )
{
	$data['top_title'] = $data['title'] = $title;
	$data['info'] = $info;
	$data['isInfo'] = true;

	render( $data );
}

if (!function_exists('apache_request_headers'))
{
	function apache_request_headers()
	{
		foreach($_SERVER as $key=>$value)
		{
			if (substr($key,0,5)=="HTTP_")
			{
				$key=str_replace(" ","-",ucwords(strtolower(str_replace("_"," ",substr($key,5)))));
                    $out[$key]=$value;
			}
			else
			{
				$out[$key]=$value;
			}
       }

	   return $out;
   }
}

function uses( $m )
{
	load( 'lib/' . basename($m)  );
}

function load( $file_path )
{
	$file = AROOT . $file_path;
	if( file_exists( $file ) )
	{
		//echo $file;
		require( $file );

	}
	else
	{
		//echo CROOT . $file_path;
		require( CROOT . $file_path );
	}

}
